//
//  MineCouponsContainerView.swift
//  LeTu
//
//  Created by g1 on 2023/12/11.
//

import UIKit

class MineCouponsContainerView: UIView {
    
    private var index = 0
    private var viewModel = MineCouponsItemViewModel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    
    static func getCellHeight() -> CGFloat {
        return screenHeight-Size.navigationBarHeight-Size.tabBarHeight-Size.safeAreaBottomGap-44
    }
    func setIndex(_ index: Int) {
        self.index = index
        viewModel.useStatus = MineCouponsUseStatus(rawValue: index-1) ?? .unGet
        
        couponsTableView.headerBeginRefreshing()
    }
    // MARK: - loadData
    private func loadData(isLoad: Bool = false,completeHolder: ((DialogMessage) -> Void)? = nil) {
        
        if isLoad == false {
            couponsTableView.footerResetNoMoreData()
        }
        
        viewModel.getCouponList(isLoad: isLoad) { [weak self] dialog in
            
            guard let strongSelf = self else { return }
            
            DispatchQueue.main.async {
                
                if dialog.code != 200 {
                    Utils.showMessage(message: dialog.message)
                }
                
                if let handle = completeHolder {
                    handle(dialog)
                }
                
                if dialog.code == 200 && (dialog.data != nil) == false {
                    strongSelf.couponsTableView.footerEndRefreshingWithNoMoreData()
                }
                strongSelf.couponsTableView.reloadData()
                
            }
            
        }
        
    }
    // 领取优惠券
    private func getCoupon(index: Int, couponsModel: MineCouponsModel) {
        
        viewModel.getCoupon(couponId: couponsModel.id) { [weak self] dialog in
            
            guard let strongSelf = self else { return }
            DispatchQueue.main.async {
                
                if dialog.code == 200 {
                    strongSelf.viewModel.goodsList.remove(at: index)
                    strongSelf.couponsTableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .fade)
                } else {
                    strongSelf.showMessage(message: dialog.message)
                }
                
            }

        }
        
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        backView.addSubview(couponsTableView)
        
        backView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        couponsTableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        couponsTableView.delegate = self
        couponsTableView.dataSource = self
        couponsTableView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
        couponsTableView.register(cellClass: BaseEmptyTableViewCell.self)
        couponsTableView.register(cellClass: MineCouponsTableViewCell.self)
        couponsTableView.addRefreshHeaderCallback { [weak self] in
            guard let strongSelf = self else { return }
            strongSelf.loadData(isLoad: false) { _ in strongSelf.couponsTableView.headerEndRefreshing() }
        }
//        couponsTableView.addRefreshFooterCallback { [weak self] in
//            guard let strongSelf = self else { return }
//            strongSelf.loadData(isLoad: true) { _ in strongSelf.couponsTableView.footerEndRefreshing() }
//        }
        
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        return backView
    }()
    private(set) lazy var couponsTableView: UITableView = {
        let couponsTableView = UITableView()
        couponsTableView.backgroundColor = .app_viewBackgroundColor
        couponsTableView.tableFooterView = UIView()
        couponsTableView.showsVerticalScrollIndicator = false
        couponsTableView.separatorStyle = .none
        return couponsTableView
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension MineCouponsContainerView: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return viewModel.goodsList.count != 0 ? 104 : tableView.height
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.goodsList.count == 0 ? 1 : viewModel.goodsList.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if viewModel.goodsList.count == 0 {
            let cell = tableView.dequeueReusableCell(BaseEmptyTableViewCell.self, indexPath: indexPath)
            cell.setInfo(iconName: "nodata", title: "暂无数据".localized)
            return cell
        }
        let cell = tableView.dequeueReusableCell(MineCouponsTableViewCell.self, indexPath: indexPath)
        cell.setCouponModel(viewModel.goodsList[indexPath.item], index: indexPath.item, useStatus: viewModel.useStatus)
        cell.clickGeCouponHandle = { [weak self] index, couponsModel in
            guard let strongSelf = self else { return }
            strongSelf.getCoupon(index: index, couponsModel: couponsModel)
        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard viewModel.goodsList.count > 0 else { return }
    }
    
}

extension MineCouponsContainerView: JXSegmentedListContainerViewListDelegate {
    
    func listView() -> UIView {
        return self
    }
    
}
